import type { UseFetchOptions } from "nuxt/app";
import request from "./request";
import type { IBody, IResponseDataType } from "./types";

const BASE_URL = import.meta.env.VITE_BASE_URL;

class Http {
  baseUrl: string;
  constructor(baseUrl: string = BASE_URL) {
    this.baseUrl = baseUrl;
  }

  get<T>(url: string, options: UseFetchOptions<IResponseDataType<T>> = {}) {
    return request<T>(this.baseUrl + url, {
      ...options,
      method: "GET",
    });
  }
  post<T>(
    url: string,
    body: IBody,
    options: UseFetchOptions<IResponseDataType<T>> = {}
  ) {
    return request<T>(this.baseUrl + url, {
      ...options,
      body,
      method: "POST",
    });
  }
  put<T>(
    url: string,
    body: IBody,
    options: UseFetchOptions<IResponseDataType<T>> = {}
  ) {
    return request<T>(this.baseUrl + url, {
      ...options,
      body,
      method: "PUT",
    });
  }
  delete<T>(url: string, options: UseFetchOptions<IResponseDataType<T>> = {}) {
    return request<T>(this.baseUrl + url, {
      ...options,
      method: "DELETE",
    });
  }
}

export const http = new Http();

const MOCK_BASE_URL = "/api";
export const httpMock = new Http(MOCK_BASE_URL);
